<template> 
  <div class="city" ref="cityview">
    <div class="select" @click="toggle()" :class="{ active: showpanel }">
      <span class="placeholder"
        >{{ fullCitydata.proname }} {{ fullCitydata.cityname }}
        {{ fullCitydata.couname }}</span
      >
      <span class="value"></span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="option" v-show="showpanel">
      <span
        class="ellipsis"
        v-for="(item, key) in list"
        :key="key"
        @click="setdata(item)"
        >{{ item.name }}</span
      >
    </div>
  </div>
</template>

<script>
import axios from "axios";
export default {
  props:['fullCitydata'],
  data() {
    return {
      showpanel: false,
      arealist: [],
      dataobj: {
        procode: null,
        proname: null,
        citycode: null,
        cityname: null,
        coucode:null,
        couname:null
      },
    };
  },
  methods: {
    //展开
    open() {
      this.showpanel = true;
      this.getData();
      //滞空
      this.dataobj= {
        procode: null,
        proname: null,
        citycode: null,
        cityname: null,
        coucode:null,
        couname:null
      }
    },
    close() {
      this.showpanel = false;
    },
    //切换
    toggle() {
      if (this.showpanel) {
        this.close();
      } else {
        this.open();
      }
    },
    async getData() {
      //数据不直接拿  在执行这个方法的时候可以先判断缓存中是否有数据 如果有直接返回 如果没有 再去用axios去请求
      if (window.area) {
        //直接拿缓存的数据
        this.arealist = window.area;
      } else {
        var url =
          "https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json";
        let res = await axios.get(url);
        window.area = res.data;
        this.arealist = res.data;
      }
    },
    //设置选中的数据
    setdata(item) {
      if (item.level == 0) {
        this.dataobj.procode = item.code;
        this.dataobj.proname = item.name;
      }
      if (item.level == 1) {
        this.dataobj.citycode = item.code;
        this.dataobj.cityname = item.name;
      }
      if (item.level == 2) {
        this.dataobj.coucode = item.code;
        this.dataobj.couname = item.name;
        this.close()
        //将收集的数据返回给父组件
        this.$emit('setFulldata',this.dataobj)
      }
    },
  },
  mounted() {
    document.addEventListener("click", (e) => {
      //获取到我们城市组件的dom元素、
      // console.log(e);
      var obj = this.$refs.cityview;
      if (!obj.contains(e.target)) {
        this.close();
      }
    });
  },
  computed: {
    list() {
      let arr = [];
      //默认的展示省份
      arr = this.arealist;
      //什么时候展示市区
      if(this.dataobj.procode!=null){
        //根据省份id找到当前的对象
        var obj = this.arealist.find(item=>{
          return item.code == this.dataobj.procode
        })
        arr = obj.areaList
      }
      //什么事时候展示县
      if(this.dataobj.citycode!=null){
        //根据省份id找到当前的对象
        var couobj = arr.find(item=>{
          return item.code == this.dataobj.citycode
        })
        arr = couobj.areaList
        // this.close()
      }

      return arr;
    },
  },
};
</script>

<style scoped lang="scss">
.city {
  display: inline-block;
  position: relative;
  z-index: 400;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>
